package com.example.smartcity.ui.smartCity.activitytModule

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.smartcity.R
import com.example.smartcity.databinding.ActivitySmViewPagerBinding
import com.example.smartcity.logic.utils.BaseActivity
import com.example.smartcity.ui.smartCity.fragment.SmFragment_1
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class SmViewPagerActivity : BaseActivity() {

    private lateinit var mBinding: ActivitySmViewPagerBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivitySmViewPagerBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        title = "党建动态"
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        // 设置TabLayout和ViewPager2
        setTabLayoutViewPager2()
    }

    /**
     * 设置TabLayout和ViewPager2
     */
    private fun setTabLayoutViewPager2() {
        mBinding.SmViewPager2.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount() = 3

            override fun createFragment(position: Int): Fragment = when(position){
                0 -> SmFragment_1()
                else -> SmFragment_1()
            }
        }

        mBinding.SmTabLayout.tabMode = TabLayout.MODE_FIXED
        TabLayoutMediator(mBinding.SmTabLayout, mBinding.SmViewPager2){ tab, position ->
            when(position){
                0 -> tab.text = "二十大"
                1 -> tab.text = "人民大会堂"
                2 -> tab.text = "新闻"
                else -> SmFragment_1()
            }
        }.attach()
    }

    /**
     * 重写返回事件
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // 销毁改意图
        this.finish()
        return super.onOptionsItemSelected(item)
    }
}